package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.converter.DictionaryConverter;
import com.neusoft.neuiotms.common.model.Dictionary;
import com.neusoft.neuiotms.common.model.DictionaryType;
import com.neusoft.neuiotms.common.repository.DictionaryRepository;
import com.neusoft.neuiotms.common.repository.DictionaryTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetExtFieldConfigDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetExtFieldConfig;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetExtFieldConfigRepository;

@Component
public class AssetExtFieldConfigConverter extends AbstractConverter<AssetExtFieldConfig, AssetExtFieldConfigDTO> {

    @Autowired
    private AssetExtFieldConfigRepository assetExtFieldConfigRepository;
    @Autowired
    private DictionaryTypeRepository dictionaryTypeRepository;
    @Autowired
    private DictionaryRepository dictionaryRepository;
    @Autowired
    private DictionaryConverter dictionaryConverter;

    @Override
    public AssetExtFieldConfig toModel(final AssetExtFieldConfigDTO dto) {
        AssetExtFieldConfig model;
        if (dto.isNew()) {
            model = new AssetExtFieldConfig();
        } else {
            model = this.assetExtFieldConfigRepository.getOne(dto.getId());
        }
        model.setSequence(dto.getSequence());
        model.setName(dto.getName());
        model.setExtColumn(dto.getExtColumn());
        model.setDataType(dto.getDataType());
        model.setDataValue(dto.getDataValue());
        model.setDefaultValue(dto.getDefaultValue());
        model.setRequired(dto.getRequired());
        model.setShowed(dto.getShowed());
        model.setStatus(dto.getStatus());
        model.setValidateRule(dto.getValidateRule());
        return model;
    }

    @Override
    public AssetExtFieldConfigDTO toDTO(final AssetExtFieldConfig model, final boolean forListView) {
        final AssetExtFieldConfigDTO dto = new AssetExtFieldConfigDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());

        dto.setSequence(model.getSequence());
        dto.setExtColumn(model.getExtColumn());
        dto.setDataType(model.getDataType());
        dto.setDataValue(model.getDataValue());
        dto.setDefaultValue(model.getDefaultValue());
        dto.setRequired(model.getRequired());
        dto.setShowed(model.getShowed());
        dto.setStatus(model.getStatus());
        dto.setValidateRule(model.getValidateRule());
        dto.setShow(model.getStatus());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        if (StringUtils.isNotEmpty(dto.getDataValue())) {
            final DictionaryType dictionaryType = this.dictionaryTypeRepository.findByType(dto.getDataValue());
            if (dictionaryType != null) {
                final List<Dictionary> models = this.dictionaryRepository.findByDictionaryType(dictionaryType);
                if (CollectionUtils.isNotEmpty(models)) {
                    dto.setExtDicData(this.dictionaryConverter.toListDTO(models));
                }
            }
        }
        return dto;
    }

}
